﻿using BaseSimpleAPI;
using CommonHelper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace UDPHelper
{
    /// <summary>
    /// UDP函数的管道
    /// </summary>
    public class UDPActionPipeline : UDPPipeline
    {
        /// <summary>
        /// 被监控的函数
        /// </summary>
        public MethodInfo Method { get; set; }

        /// <summary>
        /// 依附的Controller对象
        /// </summary>
        public BaseController Controller { get; set; }

        public override bool Handle(UDPPackage context)
        {
            if (Method == null)
                return false;

            // 函数应该被要求特定格式，否则进行反射的时候，和数据传入的时候可能会非常缓慢。
            var parameters = Method.GetParameters();

            List<object> objs = new List<object>();

            JObject net_data = (JObject)JsonConvert.DeserializeObject(context.Body.ToString());

            // 处理传入参数
            foreach (var item in parameters)
            {
                objs.Add(TypeHelper.TryGetParameter(net_data, item));
            }

            // 本地函数的反射调用
            object response_obj = Method.Invoke(Controller, objs.ToArray());

            // 处理反应
            if (response_obj != null)
            {
                if (response_obj is IActionResult)
                {
                    // 新版本数据的返回

                    IActionResult result = response_obj as IActionResult;

                    UDPBasePackage udp_package = new UDPBasePackage
                    {
                        Data = result.GetRespondData(),
                        IPAddress = context.IPAddress,
                        Port = context.Port,
                    };

                    SingleCase.Single<UDPServer>().GetUDPPackageManager().PushUDPPackageToStack(udp_package);
                }
                else
                {
                    // 老版本数据返回

                    StringBuilder response_str = new StringBuilder();

                    if (response_obj is string)
                    {
                        response_str.Append(response_obj as string);
                    }
                    else if (response_obj.GetType().IsValueType)
                    {
                        response_str.Append(response_obj.ToString());
                    }
                    else
                    {
                        response_str.Append(JsonConvert.SerializeObject(response_obj));
                    }

                    UDPBasePackage udp_package = new UDPBasePackage
                    {
                        Data = UDPPackageHelper.GetByteData(ResponseHeaderHelper.GetInstance().GetResponseHeader(), response_str.ToString()),
                        IPAddress = context.IPAddress,
                        Port = context.Port,
                    };

                    SingleCase.Single<UDPServer>().GetUDPPackageManager().PushUDPPackageToStack(udp_package);
                }

                //UDPPackageQueueHelper.GetInstance().PushUDPPackageToStack(udp_package);
            }

            return true;
        }
    }
}
